package com.sh4.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sh4.dao.MenuDao;
import com.sh4.dao.PermissionResourceDao;
import com.sh4.dao.RolePermissionDao;
import com.sh4.dao.impl.BaseSqlDaoImpl;
import com.sh4.entity.Menu;
import com.sh4.entity.RolePermission;
import com.sh4.service.MenuService;
import com.sh4.util.ContextHeader;


@Service("MenuServiceImpl")
@Transactional
public class MenuServiceImpl implements MenuService {
	
	@Autowired
	MenuDao menuDao;
	
	@Autowired
	PermissionResourceDao permissionResourceDao;
	
	@Autowired
	RolePermissionDao rolePermissionDao;
	
	@Autowired
	BaseSqlDaoImpl baseSqlDao;
	
	
	// 得到一级目录
	@Override 
	public List<Menu> allMenu(String permissionId) {

		List<Menu> list = new ArrayList<Menu>();
		List<Menu> mainMenus = menuDao.getAllMenu("0");
		for (Menu m : mainMenus) {
			list.add(getMenuWithId(m.getId(),permissionId));
		}

		return list;
	}

	// 迭代出子目录集合
	@Override
	public Menu getMenuWithId(String id , String permissionId) {
		Menu menu = menuDao.getMenuById(id,permissionId);
		List<Menu> list = menuDao.getChildMenu(menu.getId());
		for (Menu m : list) {
			Menu tmpMenu = getMenuWithId(m.getId(),permissionId);
			//判断 是否在权限控制范围内
			if(StringUtils.isNotBlank(tmpMenu.getUrl()) && !checkPermission(tmpMenu,permissionId)) {
				System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"+tmpMenu.getUrl()+">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			}else {
				menu.getChildrenMenu().add(tmpMenu);
			}
		}

		return menu;
	}
	
	/***
	 * 获取不带权限的书 用于配置权限
	 */
	public List<Menu> allMenu() {

		List<Menu> list = new ArrayList<Menu>();

		List<Menu> mainMenus = menuDao.getAllMenu("0");

		for (Menu m : mainMenus) {

			list.add(getMenuWithId(m.getId()));
		}

		return list;
	}

	/***
	 * 获取不带权限的书 用于配置权限
	 */
	@Override
	public Menu getMenuWithId(String id) {

		Menu menu = menuDao.getMenuById(id,"");

		List<Menu> list = menuDao.getChildMenu(menu.getId());

		for (Menu m : list) {
			Menu tmpMenu = getMenuWithId(m.getId());
			menu.getChildrenMenu().add(tmpMenu);
		}

		return menu;
	}
	

	@Override
	public void saveMenu(Menu m) {
		if(m.getId() == null) {
			m.setCreatPerson(ContextHeader.getUserCode());
			m.setCreatTime(new Date());
		}else {
			m.setUpdateTime(new Date());
			m.setUpdatePerson(ContextHeader.getUserCode());
		}
		menuDao.saveOrUpdate(m);
	}

	@Override
	public void deleteMenu(Menu m) {
		menuDao.delete(m);
	}
	
	/***
	 * 根据 role 获取权限id 在根据权限id 去检索可用菜单
	 */
	@Cacheable(value="menus" ,key="#roleId")
	@Override
	public List<Menu> permissionMenu(String roleId){
		
		//根据roleId 获取权限 id
		
		RolePermission p = rolePermissionDao.queryRp(roleId);
		
		String id = p.getPermissionId();
		
		return allMenu(id);
	}
	
	public Boolean checkPermission(Menu menu,String permissionId) {
		
		List<String> pers = permissionResourceDao.getPermissionResources(permissionId);
		
		for(String per : pers) {
			if(menu.getId().equals(per)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public  List<Map<String, Object>> testSql() {
		
		return baseSqlDao.getArrayBySql("select r.ROLE_ID_ , e.ROLE_NAME_ from role_permission r left join role e on(r.ROLE_ID_ = e.ID_)");
	}
}
